package com.example.geoquiz;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;

import android.content.Context;
import android.content.Intent;
import android.nfc.Tag;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import static com.example.geoquiz.QuestionBank.mQuestions;

public class MainActivity extends AppCompatActivity {
    private static final String INDEX = "INDEX";
    private static final String START_INDEX = "START_INDEX";
//    Button mTrueBtn;
//    Button mFalseBtn;
//    Button mNextBtn;
//    TextView mTextView;
//
    int mIndex = 0;
    private String TAG="lijifei";

    @Override
    protected void onDestroy(){
        super.onDestroy();
        Log.d(TAG, "onDestroy called");
    }

    public static Intent newIntent(Context contex, int position){
        Intent it = new Intent(contex, MainActivity.class);
        it.putExtra(START_INDEX, position);
        return it;
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt(INDEX, mIndex);
        Log.d(TAG, "onSaveInstanceState called, mIndex: "+mIndex);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d(TAG, "onCreate called");

        Intent it = getIntent();
        mIndex = it.getIntExtra(START_INDEX, 0);

        if (savedInstanceState != null){
            mIndex = savedInstanceState.getInt(INDEX, 0);
        }

        FragmentManager fm = getSupportFragmentManager();
        Fragment f = fm.findFragmentById(R.id.fragmentContainer);
        if (f==null){
            f = QuizFragment.newInstance(mIndex);//new QuizFragment();
            fm.beginTransaction().add(R.id.fragmentContainer, f).commit();
        }

//        Spinner mSpinner = findViewById(R.id.spinner);
//        String[] mHuman = getResources().getStringArray(R.array.human);
//        ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, mHuman);
//        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//        mSpinner.setAdapter(adapter);
//        mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
//            @Override
//            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//                String current = mHuman[position];
//                Toast.makeText(MainActivity.this, current, Toast.LENGTH_SHORT).show();
//                Log.d("ljf", current);
//                for (int i=0; i<mQuestions.length; i++){
//                    int resId = mQuestions[i].getResId();
//                    String ques = getString(resId);
//                    if (ques.contains(current)) {
//                        mIndex = i;
//                        mTextView.setText(resId);
//                    }
//                }
//            }
//
//            @Override
//            public void onNothingSelected(AdapterView<?> parent) {
//
//            }
//        });
//
//        mTrueBtn = findViewById(R.id.trueButton);
//        if (mTrueBtn != null) {
//            mTrueBtn.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View view) {
//                    checkAnswer(true);
//                    //Toast.makeText(MainActivity.this,"gongxi", Toast.LENGTH_SHORT).show();
//                }
//            });
//        }
//
//        mFalseBtn = findViewById(R.id.falseButton);
//        if (mFalseBtn != null) {
//            mFalseBtn.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View view) {
//
//                    checkAnswer(false);
//                    //Toast.makeText(MainActivity.this,"err", Toast.LENGTH_SHORT).show();
//                }
//            });
//        }
//
//        mTextView = findViewById(R.id.questionView);
//        if (mTextView != null) {
//            int strId = mQuestions[mIndex].getResId();
//            mTextView.setText(strId);
//        }
//
//        mNextBtn = findViewById(R.id.nextButton);
//        if (mNextBtn != null) {
//            mNextBtn.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View view) {
//                    mIndex = (mIndex + 1) % mQuestions.length;
//                    int strId = mQuestions[mIndex].getResId();
//                    mTextView.setText(strId);
//                }
//            });
//        }

    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState){
        super.onRestoreInstanceState(savedInstanceState);
        Log.d(TAG, "onRestoreInstanceState called");
    }

    @Override
    protected void onStart(){
        super.onStart();
        Log.d(TAG, "onStart called");
    }

    @Override
    protected void onPause(){
        super.onPause();
        Log.d(TAG, "onPuse called");
    }

    @Override
    protected void onResume(){
        super.onResume();
        Log.d(TAG, "onResume called");
    }

    @Override
    protected void onStop(){
        super.onStop();
        Log.d(TAG, "onStop called");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.d(TAG, "onRestart called");
    }

//    private void checkAnswer(boolean b) {
//        boolean bAns = mQuestions[mIndex].isAnswerTrue();
//        if(bAns == b){
//            Toast.makeText(MainActivity.this, "gongxi", Toast.LENGTH_SHORT).show();
//        }
//        else{
//            Toast.makeText(MainActivity.this, "err", Toast.LENGTH_SHORT).show();
//        }
//    }
}